package com.example.log.util;

import java.io.IOException;
import java.util.Date;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;



/**
 * 过滤所有的连接，进行签名验证 get请求之前验证，post请求之后验证
 * 
 * @author Administrator
 */
@WebFilter(filterName = "signatureFilter", urlPatterns = "/*")
@Slf4j
public class SignatureFilter implements Filter {

	private final String testToken = "8a22b2555437907401544c09f8ef0001";

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String method = req.getMethod();
		String requestURI = req.getRequestURI();
		// 当前时间
		Long nowTime = new Date().getTime();
		try {
			// 跳过验证
			String devToken = req.getParameter("testToken");

			LogRequestWrapper requestWrapper = new LogRequestWrapper(req);
			
			log.info(requestWrapper.getLogString());
			req = requestWrapper;

			

		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

	
	/**
	 * 图片不做签名处理
	 * 
	 * @param allMap
	 */
	public static void removeKey(Map allMap) {
		if (allMap.containsKey("image")) {
			allMap.remove("image");
		}
		if (allMap.containsKey("icon")) {
			allMap.remove("icon");
		}
		if (allMap.containsKey("groupIoc")) {
			allMap.remove("groupIoc");
		}
		if (allMap.containsKey("msgFile")) {
			allMap.remove("msgFile");
		}
		if (allMap.containsKey("file")) {
			allMap.remove("file");
		}
		if (allMap.containsKey("avatar")) {
			allMap.remove("avatar");
		}
		if (allMap.containsKey("homebag")) {
			allMap.remove("homebag");
		}
		if (allMap.containsKey("img")) {
			allMap.remove("img");
		}
		if (allMap.containsKey("img0")) {
			allMap.remove("img0");
		}
		if (allMap.containsKey("img1")) {
			allMap.remove("img1");
		}
		if (allMap.containsKey("img2")) {
			allMap.remove("img2");
		}
		if (allMap.containsKey("img3")) {
			allMap.remove("img3");
		}
		if (allMap.containsKey("img4")) {
			allMap.remove("img4");
		}
		if (allMap.containsKey("img5")) {
			allMap.remove("img5");
		}
		if (allMap.containsKey("img6")) {
			allMap.remove("img6");
		}
		if (allMap.containsKey("img7")) {
			allMap.remove("img7");
		}
		if (allMap.containsKey("img8")) {
			allMap.remove("img8");
		}
		if (allMap.containsKey("img9")) {
			allMap.remove("img9");
		}
		if (allMap.containsKey("photo")) {
			allMap.remove("photo");
		}
		if (allMap.containsKey("taskUploadScreenShot")) {
			allMap.remove("taskUploadScreenShot");
		}
	}

}
